package com.yingdong.robot;

import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yingdong.opcode.ServerOpcode;
import com.yingdong.sceneserver.inner.message.Message;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

public class RobotClientHandler extends ChannelInboundHandlerAdapter {
	static Log logger = LogFactory.getLog(RobotClientHandler.class);
	private final AttributeKey<Robot> ROBOT = AttributeKey.valueOf("ROBOT");

	@Override
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {

	}

	@Override
	public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {

	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		
		Attribute<Robot> attribute = ctx.attr(ROBOT);
		Robot robot = new Robot();
		robot.setAccName(UUID.randomUUID().toString());
		attribute.set(robot);
		new RobotMessage(ServerOpcode.LOGIN_OK).writeString(robot.getAccName()).writeString("").sendToServer(ctx.channel());
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {

	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		Attribute<Robot> attribute = ctx.attr(ROBOT);
		Robot robot = attribute.get();
		ByteBuf buf = (ByteBuf) msg;
		int len = buf.readInt();
		int opcode = buf.readInt();
		switch (opcode) {
		case ServerOpcode.LOGIN_OK:
			int result = buf.readByte();
			buf.readLong();
			if (result == 1) {
				int accId = buf.readInt();
				robot.setAccId(accId);
				int userId = buf.readInt();
				if (userId == 0) {
					robot.setUserName(UUID.randomUUID().toString());
					new RobotMessage(ServerOpcode.CREATE_ROLE).writeInt(accId).writeString(robot.getUserName()).writeByte(0).sendToServer(ctx.channel());

				} else {
					new RobotMessage(ServerOpcode.ENTER_SCENE).writeInt(userId).sendToServer(ctx.channel());
				}
			}
			break;
		case ServerOpcode.CREATE_ROLE:
			result = buf.readByte();
			if (result == 1) {
				int userId = buf.readInt();
				new RobotMessage(ServerOpcode.ENTER_SCENE).writeInt(userId).sendToServer(ctx.channel());
			}
			break;
		case ServerOpcode.USER_INFO:
			int userId = buf.readInt();
			if (userId != 0) {
				String userName = Message.readString(buf);
				int gender = buf.readByte();

				new RobotMessage(ServerOpcode.READY).sendToServer(ctx.channel());
				new RobotMessage(ServerOpcode.HEARTBEAT).sendToServer(ctx.channel());
				logger.info("角色"+userName+"进入游戏");
				//ctx.channel().disconnect();
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

	}

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

	}

	@Override
	public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {

	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

	}

}
